<template>
	<div>
		<div v-if="list.length > 0">
			<div v-for="(item, index) in list">
				<keep-alive>
					<Lessor
						v-if="includes(1, item.changeType)"
						ref="Lessor"
						v-model="list[index]"
						v-bind="querys"
						:deptList="deptList"></Lessor>
				</keep-alive>
				<!-- 承租方 -->
				<keep-alive>
					<Leasee
						v-if="includes(2, item.changeType)"
						ref="Leasee"
						v-model="list[index]"
						v-bind="querys"></Leasee>
				</keep-alive>
				<keep-alive>
					<Car
						v-if="includes(8, item.changeType)"
						ref="Car"
						v-model="list[index]"
						v-bind="querys"></Car>
				</keep-alive>
				<!-- 期限调整 -->
				<keep-alive>
					<Time
						v-if="includes(4, item.changeType)"
						ref="Time"
						v-model="list[index]"
						v-bind="querys"></Time>
				</keep-alive>
				<!-- 租金调整 -->
				<keep-alive>
					<Rent
						ref="Rent"
						v-if="includes(32, item.changeType)"
						v-model="list[index]"
						v-bind="querys"></Rent>
				</keep-alive>
				<!-- 维保 -->
				<keep-alive>
					<Maintain
						ref="Maintain"
						v-if="includes(128, item.changeType)"
						v-model="list[index]"
						v-bind="querys"></Maintain>
				</keep-alive>

				<!-- 替车 -->
				<keep-alive>
					<Replace
						ref="Replace"
						v-if="includes(256, item.changeType)"
						v-model="list[index]"
						v-bind="querys"></Replace>
				</keep-alive>
				<!-- 保险 -->
				<keep-alive>
					<Insurance
						ref="Insurance"
						v-if="includes(64, item.changeType)"
						v-model="list[index]"
						v-bind="querys"></Insurance>
				</keep-alive>
				<!-- 加装 -->
				<keep-alive>
					<Decoration
						ref="Decoration"
						v-if="includes(512, item.changeType)"
						v-model="list[index]"
						v-bind="querys"></Decoration>
				</keep-alive>
				<!-- 其他 -->
				<keep-alive>
					<Other
						ref="Other"
						v-if="includes(1024, item.changeType)"
						v-model="list[index]"
						v-bind="querys"></Other>
				</keep-alive>
			</div>
		</div>
		<div v-else>
			<el-empty description="暂无变更记录！"></el-empty>
		</div>
	</div>
</template>

<script>
	import * as app from '@/api/app.js';
	import * as apis from '@/api/order/minOrder/contractChange/api.js';
	import Leasee from '@/views/order/minOrder/chunk/adjust/leasee/index.vue';
	import Lessor from '@/views/order/minOrder/chunk/adjust/lessor/index.vue';
	import Time from '@/views/order/minOrder/chunk/adjust/time/index.vue';
	import Rent from '@/views/order/minOrder/chunk/adjust/rent/index.vue';
	import Car from '@/views/order/minOrder/chunk/adjust/car/index.vue';
	import Maintain from '@/views/order/minOrder/chunk/adjust/maintain/index.vue';
	// import LeaseeApply from '@/views/order/minOrder/chunk/apply/leasee/index.vue';
	// import Law from '@/views/order/minOrder/chunk/apply/law/index.vue';
	// import Car from '@/views/order/minOrder/chunk/apply/car/index.vue';
	import Replace from '@/views/order/minOrder/chunk/adjust/replace/index.vue';
	import Insurance from '@/views/order/minOrder/chunk/adjust/insurance/index.vue';
	import Decoration from '@/views/order/minOrder/chunk/adjust/decoration/index.vue';
	import Other from '@/views/order/minOrder/chunk/adjust/other/index.vue';
	export default {
		name: '',
		components: {
			Leasee,
			Lessor,
			Time,
			Rent,
			Maintain,
			Car,
			Replace,
			Insurance,
			Decoration,
			Other,
		},
		dicts: ['order_car_change_type'],
		props: {},
		data() {
			return {
				list: [],
				querys: {
					state: 'readonly',
				},
				deptList: {
					company: [],
				},
				dictList: {
					contractTemplateId: [],
					levelList: [],
					supplierList: [],
					packagelList: [],
					refittedList: [],
					changeType: [],
				},
			};
		},
		provide() {
			return {
				dictList_provide: () => this.dictList,
			};
		},
		computed: {},
		created() {
			this.getDetails();
			this.getDeptList(2);
			this.getLevelList();
			this.getPackagelList();
			this.getSupplierList();
			this.getInsuranceList();
		},
		mounted() {},
		methods: {
			includes(type, list = this.formData.changeType) {
				let pass = true;
				if (typeof list === 'string' || typeof list === 'number') {
					list = list?.toString()?.split(',');
				}
				let types = type.toString().split(',');
				if (!Array.isArray(list)) {
					return false;
				}

				for (let index = 0; index < types.length; index++) {
					const element = types[index];
					pass = list?.includes(element.toString());
					if (!pass) {
						break;
					}
				}
				return pass;
			},
			getDetails() {
				apis.OrderChangeDetail(this.$route.query.id).then(res => {
					console.log(res);
					// res.data?.map(row => {
					// 	let changeType = [];
					// 	this.dict.type.order_car_change_type?.map(item => {
					// 		console.log(row.changeType & (item.value * 1));
					// 		if ((row.changeType & (item.value * 1)) > 0) {
					// 			changeType.push(item.value);
					// 		}
					// 	});
					// 	row.changeType = changeType;
					// });
					this.list = res.data ?? [];
					console.log(this.list);
				});
			},

			// 加装
			getDecorationList(carSeating, type) {
				if (type !== 'init') {
					this.$set(
						this.formData.subsequentData,
						'attachedDecorationList',
						[],
					);
				}
				if (!carSeating) {
					this.dictList.refittedList = [];
					this.$set(
						this.formData.subsequentData,
						'attachedDecorationList',
						[],
					);
					return;
				}
				let params = {
					seat: carSeating,
				};
				apis.Decoration(params).then(res => {
					if (res.code == 200) {
						let refittedList = [];
						refittedList = res.data;
						refittedList.push({
							decorationName: '其他',
							decorationFee: undefined,
						});
						this.dictList.refittedList = refittedList;
					}
				});
			},
			// 商业险险种
			getInsuranceList() {
				apis.Insurance({})
					.then(res => {
						if (res.code == 200) {
							this.dictList.insuranceList = res.data ?? [];
						}
					})
					.finally(() => {});
			},
			getSupplierList() {
				let params = {
					businessTypeCode: '6',
				};
				apis.SupplierlList(params).then(res => {
					if (res.code == 200) {
						this.dictList.supplierList = res.data ?? [];
					}
				});
			},
			getLevelList() {
				apis.LevelList({}).then(res => {
					if (res.code == 200) {
						this.dictList.levelList = res.data;
					}
				});
			},
			getPackagelList() {
				apis.PackagelList({}).then(res => {
					if (res.code == 200) {
						this.dictList.packagelList = res.data;
					}
				});
			},

			preview(row) {
				if (!this.isNull(row.contractTemplateId)) {
					this.$refs.commonContractContent.show(
						row.contractTemplateId,
					);
				} else {
					this.$message.error('模版id为空,无法查看');
				}
			},
			getTemplateList(type) {
				let params = {
					templateClassification: type,
					relatedCustomers: this.formData.leaseeId,
				};
				app.TemplateList(params).then(res => {
					if (res.code == 200) {
						this.dictList.contractTemplateId = res.data;
					}
				});
			},
			getDeptList(deptType) {
				let params = {
					deptType,
				};
				app.DeptListNoPermission(params).then(res => {
					if (res.code == 200) {
						// res?.data?.map(item => {
						// 	item.deptId = item.deptId?.toString();
						// });
						switch (deptType) {
							case 1:
								this.deptList.department = res?.data ?? [];
								break;
							case 2:
								this.deptList.company = res?.data ?? [];
								break;
						}
					}
				});
			},
		},
	};
</script>

<style lang="scss" scoped></style>
